/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edmo.fastener.parser;

import com.edmo.fastener.business.CheckFiles;
import com.edmo.fastener.business.CryptoKeyGenerator;
import com.edmo.fastener.business.ListofCustomersnSuppliers;
import com.edmo.fastener.domain.Order;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.crypto.Cipher;
import javax.crypto.SealedObject;
import javax.crypto.SecretKey;

/**
 *
 * @author obelog
 */
public class ParseOrder  {
    
    private static List<Order> orders = new ArrayList<Order>();
    
    public List<Order> readnParse(String file){
        String[] arr = readFile(file).split("\n");
        for (String s : arr){
            parse(s);
        }
        return orders;
    }
    
    private String readFile(String file){
        String text = "";
        try {
            SecretKey key = ListofCustomersnSuppliers.getKey();
            SealedObject sealedObject = (SealedObject) CryptoKeyGenerator.readFromFile(file);
            String algorithmName = sealedObject.getAlgorithm();
            Cipher cipher = Cipher.getInstance(algorithmName);
            cipher.init(Cipher.DECRYPT_MODE, key);
            text = (String) sealedObject.getObject(cipher);
            CryptoKeyGenerator.writeToFile(file, text);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(ParseOrder.class.getName()).log(Level.SEVERE, null, ex);
        }
        return text;
    }


    public void parse(String s) {
        String[] arr = s.split("\\|");
        if (arr.length > 1){
        Order list = new Order(Integer.valueOf(arr[0]), Integer.valueOf(arr[1]), arr[2], arr[3]);
        orders.add(list);
        }
    }    
    
}
